package com.zhanghe.study.mapreduce.friend;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

/**
 * @author zh
 * @date 2021/4/2 14:42
 */
public class FriendMapper extends Mapper<LongWritable,Text,Text, Text> {

    Text k = new Text();
    Text v = new Text();

    // 先列出来某个人都是谁的好友
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // A:B,C,D,F,E,O
        // B:A,C,E,K
        // C:F,A,D,I
        // 人   所拥有的朋友
        String[] fields = value.toString().split(":");
        v.set(fields[0]);
        if(StringUtils.isNotBlank(fields[1])){
            String[] friends = fields[1].split(",");
            for(String friend : friends){
                k.set(friend);
                context.write(k,v);
            }
        }
    }
}
